/**
 * author:Administrator
 * date:2016/1/7
 * desc:TODO
 */

'use strict';
var debugLog = require('./function.js').debugLog;
debugLog('Load O2O: ' + module.id);

let F = require('./function');
let service = require('./dao/checkout.js');

var api = {};
exports.api = api;

api.test = function*() {
  return 'works';
};

/**
 * 添加一条 记录，用于测试
 * post /o2o_service/checkout/v1/add
 {
   "vid":"123",
   "begin":"2015-01-01 00:00:00",
   "end":"2015-01-01 23:59:59",
   "origin_income":1000
 }
 * @param env
 */
api.add = function*(env) {
  function checkArgs(args) {
    var checkResult = {args: {}};
    // 所需参数是否都有
    var needArgs = ['vid', 'begin', 'end', 'origin_money'];
    for (var key of needArgs) {
      if (typeof args[key] === 'undefined') {
        checkResult.errmsg = '缺少参数' + key;
        return checkResult;
      }
      checkResult.args[key] = args[key];
    }

    try {
      new Date(args.begin);
      new Date(args.end);
    } catch (e) {
      checkResult.errmsg = '日期格式不正确,请使用 yyyy-mm-dd HH:MM:ss';
      return checkResult;
    }
    // origin_income 正浮点
    // commission 浮点，非负
    return checkResult;
  }

  var argCheckResult = checkArgs(env.I);
  if (argCheckResult.errmsg) return F.returnMsg(400, argCheckResult.errmsg, 3);

  var retObj = yield service.add(argCheckResult.args);
  return F.returnMsg(200, 'ok', 1, retObj);
};

/**
 * 查询 结算记录
 * post /o2o_service/checkout/v1/find
 {
   "vid":"1",
   "begin":"2015-02-01 00:00:00",
   "end":"2015-02-28 23:59:59",
   "status":"unchecked"
 }
 * @param env
 */
api.find = function*(env) {
  function checkArgs(args) {
    var checkResult = {args: {}};
    // 所需参数是否都有
    var needArgs = ['vid', 'begin', 'end', 'status'];
    for (var key of needArgs) {
      checkResult.args[key] = args[key];
    }

    try {
      new Date(args.begin);
      new Date(args.end);
    } catch (e) {
      checkResult.errmsg = '日期格式不正确,请使用 yyyy-mm-dd HH:MM:ss';
      return checkResult;
    }
    // status = checked/ unchecked
    return checkResult;
  }

  var argCheckResult = checkArgs(env.I);
  if (argCheckResult.errmsg) return F.returnMsg(400, argCheckResult.errmsg, 3);

  var retObj = yield service.find(argCheckResult.args);
  return F.returnMsg(200, 'ok', 1, retObj);
};

/**
 * 搜索
 * 一个时间段内
 * 一个商家
 * 一个项目的
 * 结算详情
 * post /o2o_service/checkout/v1/detail
 {
   "vid":"3",
   "begin":"2015-01-01 00:00:00",
   "end":"2016-01-01 23:59:59",
   "wareid":32
 }
 */
api.detail = function*(env) {
  function checkArgs(args) {
    var checkResult = {args: {}};
    var needArgs = ['vid', 'begin', 'end', 'wareid'];
    for (var key of needArgs) {
      checkResult.args[key] = args[key];
    }
    try {
      new Date(args.begin);
      new Date(args.end);
    } catch (e) {
      checkResult.errmsg = '日期格式不正确,请使用 yyyy-mm-dd HH:MM:ss';
      return checkResult;
    }
    return checkResult;
  }

  var argCheckResult = checkArgs(env.I);
  if (argCheckResult.errmsg) return F.returnMsg(400, argCheckResult.errmsg, 3);

  var retObj = yield service.detail(argCheckResult.args);
  return F.returnMsg(200, 'ok', 1, retObj);
};

/**
 * 设置 结算记录的状态
 * post /o2o_service/checkout/v1/setStatus
 * 如果id是 list,则批量修改
 * uncheked,checking,checked
 {
   "id":"1",
   "status":"unchecked"
 }
 * @param env
 */
api.setStatus = function*(env) {
  function checkArgs(args) {
    var checkResult = {args: {}};
    // 所需参数是否都有
    var needArgs = ['id', 'status'];
    for (var key of needArgs) {
      checkResult.args[key] = args[key];
    }
    // status 枚举值 uncheked,checking,checked
    return checkResult;
  }

  var argCheckResult = checkArgs(env.I);
  if (argCheckResult.errmsg) return F.returnMsg(400, argCheckResult.errmsg, 3);

  var retObj = yield service.setStatus(argCheckResult.args);
  return F.returnMsg(200, 'ok', 1, retObj);
};

/**
 * 对所有商家 某个月进行结算
 * 必须 此api的调用时间，必须在 (year-month 之后)
 * post /o2o_service/checkout/v1/checkOneMonth
 {
   "year":"2015",
   "month":"1"
 }
 * @param year
 * @param month 1-based
 */
api.checkOneMonth = function*(env) {
  function checkArgs(args) {
    var checkResult = {args: {}};
    // 所需参数是否都有
    var needArgs = ['year', 'month'];
    for (var key of needArgs) {
      if (typeof args[key] === 'undefined') {
        checkResult.errmsg = '缺少参数' + key;
        return checkResult;
      }
      checkResult.args[key] = args[key];
    }


    // month 1~12, year
    var now = new Date();
    var __BASE_YEAR = 1900;
    var __BASE_MONTH = 1;
    var thisYear = now.getYear() + __BASE_YEAR;
    var year = parseInt(checkResult.args.year);
    if (thisYear < year) {
      checkResult.errmsg = '年份超前:' + checkResult.args.year + ' 还不能结算';
      return checkResult;
    }

    var thisMonth = now.getMonth() + __BASE_MONTH; // 0-based
    var month = parseInt(checkResult.args.month);
    if (thisYear === year && thisMonth < month + 1) {
      checkResult.errmsg = '月份超前:' + checkResult.args.year + ' 还不能结算';
      return checkResult;
    }
    return checkResult;
  }

  var argCheckResult = checkArgs(env.I);
  if (argCheckResult.errmsg) return F.returnMsg(400, argCheckResult.errmsg, 3);

  var retObj = yield service.checkOneMonth(argCheckResult.args);
  return F.returnMsg(200, 'ok', 1, retObj);
};